package math

import (
	"fmt"
	"github.com/ixre/gof/math"
	nativemath "math"
	"strconv"
	"strings"
)

const MIN = 0.000001

// MIN 为用户自定义的比较精度
func IsEqual(f1, f2 float64) bool {
	return nativemath.Dim(f1, f2) < MIN
}

func FormatFloat(f float32) string {
	s := fmt.Sprintf("%.2f", f)
	if strings.HasSuffix(s, ".00") {
		return s[:len(s)-3]
	} else if strings.HasSuffix(s, "0") {
		return s[:len(s)-1]
	}
	return s
}

func ToDiscountStr(discount int) string {
	if discount == 0 || discount == 100 {
		return ""
	}
	s := strconv.Itoa(discount)
	if s[:1] == "0" {
		return s[:1]
	}
	return s[:1] + "." + s[1:]
}

func RoundAmount(amount float32) float32 {
	return math.Round32(amount, 2)
}
